package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.*;
import com.luo.service.AddressService;
import com.luo.service.CartService;
import com.luo.service.SummaryService;
import com.luo.util.CountDown;
import com.luo.util.TokenId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：生成订单
 * 作者：Luo。
 * 日期：2024/3/22 9:12
 */
@WebServlet("/member/order/pre")
public class GetCheckInfoController extends HttpServlet {

	AddressService addressService = new AddressService();
	CartService cartService = new CartService();
	SummaryService summaryService = new SummaryService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 根据用户token获取用户id
		Object userId = TokenId.getUserId(req, resp);
		if (!(userId instanceof String)) {
			return;
		}
		// 获取到订单地址列表
		List<Address> userAddresses = addressService.queryAddressByUserId((String) userId);
		// 获取订单商品集合
		List<CartsVO> goods = cartService.queryCartsVOByUserId((String) userId);
		// 计算订单总计信息
		Summary summary = summaryService.getSummary(userAddresses, goods);
		SummaryResultVO summaryResultVO = new SummaryResultVO(userAddresses, goods, summary);

		ResultObj resultObj = new ResultObj();
		resultObj.setCode("200");
		resultObj.setMsg("查询成功");
		resultObj.setResult(summaryResultVO);

		String jsonString = JSON.toJSONString(resultObj);

		resp.getWriter().write(jsonString);
		resp.getWriter().close();
	}
}
